<?php
/**
 * Created by PhpStorm.
 * User: lenovo
 * Date: 2016/11/20
 * Time: 19:58
 */
header('Content-type:textml;charset=uft-8');
$link=mysqli_connect('localhost','root','','itacst');
if(!$link){
    die('Connect Error (' . mysqli_connect_errno() . ') '
        . mysqli_connect_error());
}
mysqli_query($link,'set names utf8');
mysqli_query($link,'use `itacst`');
$sql='select * from `emp_info`';
$result=mysqli_query($link,$sql);
if(!$result) die(mysqli_error($link));
$emp_info=array();
while($row=mysqli_fetch_assoc($result)){
    $emp_info[]=$row;
}
$fields=array('e_dept','date_of_entry');
$sql_order='';
$order=isset($_GET['order']) ?$_GET['order']:'';
$sort=isset($_GET['sort']) ?$_GET['sort']:'';
if(in_array($order,$fields)){
    if($sort=='desc'){
        $sql_order="order by $order desc";
        $sort='asc';
    }else{
        $sql_order="order by $order asc";
        $sort='desc';
    }
}
$sql = "select * from `emp_info` $sql_order";
$result = mysqli_query($link,$sql);
if(!$result) die(mysqli_error($link));
$emp_info = array();
while($row = mysqli_fetch_assoc($result)){
    $emp_info[] = $row;
}
if(isset($_GET['keyword'])){
    $keyword=$_GET['keyword'];
    $where="where e_name like '%$keyword%'";
}
$sql="select *from `emp_info` $where";
$res=mysqli_query($link,$sql);
$emp_info=array();
while($row=mysqli_fetch_assoc($res)){
    $emp_info[]=$row;
}
$page_size=4;
$res=mysqli_query($link,'select count(*) from `emp_info`');
$count=mysqli_fetch_row($res);
$count = $count[0];
$max_page = ceil($count/$page_size);
$page = isset($_GET['page']) ? intval($_GET['page']) : 1;
$page = $page > $max_page ? $max_page : $page;
$page = $page < 1 ? 1 : $page;
$page_html="<a href='./showList.php?page=1'>首页</a>&nbsp;";
$page_html .="<a href='./showList.php?page=".(($page-1)>0?($page-1):1)."'>上一页</a>&nbsp;";
$page_html .="<a href='./showList.php?page=".(($page+1)<$max_page?($page+1):$max_page)."'>下一页</a>&nbsp;";
$page_html .="<a href='./showList.php?page={$max_page}'>尾页</a>";
$lim=($page-1)*2;
$sql="select * from `emp_info` limit {$lim},4";
$result=mysqli_query($link,$sql);
if(!$result) die('.mysqli_error()');
$emp_info = array();
while($row = mysqli_fetch_assoc($res)){
    $emp_info[] = $row;
}
define('APP','itacst');
require 'list_html.php';